/*
 * Copyright (C) 2012  E-dance Studio Harbin Institute of Technology 
 * Project:campusmobile
 * Author: 初楷博
 * Create Date: 2012-5-30上午09:33:38
 */
package hit.edance.campusmobile.chat.listener;

import hit.edance.campusmobile.chat.adapter.FriendsExpandableListAdapter;
import hit.edance.campusmobile.chat.model.FriendsManager;
import hit.edance.campusmobile.chat.model.User;
import hit.edance.campusmobile.chat.util.StringUtil;

import java.util.Collection;

import org.jivesoftware.smack.RosterListener;
import org.jivesoftware.smack.packet.Presence;

import android.content.Context;
import android.os.Handler;
import android.util.Log;
/**   
 * MyRosterListener
 * 
 * @Project campusmobile
 * @Package hit.edance.campusmobile.chat.listener
 * @Class MyRosterListener
 * @Date 2012-5-30 上午09:33:38
 * @author 初楷博
 * @version 1.0
 * @since 1.0
 */
public class MyRosterListener implements RosterListener{
	
	private Context context;
	private Handler myhandler;
	private FriendsExpandableListAdapter friendsAdapter;
	
	public MyRosterListener(Context contect,FriendsExpandableListAdapter friendsAdapter,Handler myhandler){
		this.context = contect;
		this.myhandler = myhandler;
		this.friendsAdapter = friendsAdapter;
	}

	/* @see org.jivesoftware.smack.RosterListener#entriesAdded(java.util.Collection)
	 *entriesAdded
	 */
	public void entriesAdded(Collection<String> addresses) {
		// TODO Auto-generated method stub
		
	}

	/* @see org.jivesoftware.smack.RosterListener#entriesUpdated(java.util.Collection)
	 *entriesUpdated
	 */
	public void entriesUpdated(Collection<String> addresses) {
		// TODO Auto-generated method stub
		
	}

	/* @see org.jivesoftware.smack.RosterListener#entriesDeleted(java.util.Collection)
	 *entriesDeleted
	 */
	public void entriesDeleted(Collection<String> addresses) {
		// TODO Auto-generated method stub
		
	}

	/* @see org.jivesoftware.smack.RosterListener#presenceChanged(org.jivesoftware.smack.packet.Presence)
	 *presenceChanged
	 */
	public void presenceChanged(Presence presence) {
		String jid = StringUtil.getLeftString(presence.getFrom(), "/");
		for (String key : FriendsManager.getInstance().getGroupChildMap()
				.keySet()) {
			for (User user : FriendsManager.getInstance().getGroupChildMap()
					.get(key)) {
				if (user.getJabberId().equals(jid)) 
					user.setStatus(presence.getStatus());
			}

		}
		FriendsManager.getInstance().sortGroupChildMap();
		myhandler.post(new Runnable() {
			public void run() {
				friendsAdapter.notifyDataSetChanged();
			}

		});

	}

}
